package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.SkillExcludeTeacher;
import com.xmy.cultivate.service.ISkillExcludeTeacherService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 排除需要统计的教师 前端控制器
 * </p>
 *
 * @author wangyuezhao
 * @since 2025-08-28
 */
@RestController
@RequestMapping("/api/skillExcludeTeacher")
public class SkillExcludeTeacherController {

    @Autowired
    private ISkillExcludeTeacherService iSkillExcludeTeacherService;

    @GetMapping("/pageList")
    public Result pageList(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "type") String type,
            @RequestParam(value = "itemsId") String itemsId,
            @RequestParam(value = "yearPart", required = false) String yearPart,
            @RequestParam(value = "quarterNum", required = false) String quarterNum
    ) {
        IPage<SkillExcludeTeacher> ipage = new Page<>(pageNum, pageSize);
        QueryWrapper<SkillExcludeTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*", "getStaffNameForId(teacher_id) as teacher_name");
        queryWrapper.eq("type", type);
        queryWrapper.eq("items_id", itemsId);
        queryWrapper.eq(StringUtils.isNotBlank(yearPart), "year_part", yearPart);
        queryWrapper.eq(StringUtils.isNotBlank(quarterNum), "quarter_num", quarterNum);
        ipage = iSkillExcludeTeacherService.page(ipage, queryWrapper);
        return new Result(Code.GET_OK, new PageResult<>(ipage.getRecords(), ipage.getTotal(), ipage.getPages()), Code.GET_OK_MSG);
    }

    @PostMapping("/save")
    public Result save(@RequestBody SkillExcludeTeacher skillExcludeTeacher) {
        if (checkRepeat(skillExcludeTeacher)) {
            return new Result(Code.UPDATE_ERROR, null, "已存在该教师，请勿重复添加");
        }
        boolean save = iSkillExcludeTeacherService.save(skillExcludeTeacher);
        return new Result(save ? Code.SAVE_OK : Code.SAVE_ERROR, null, save ? Code.SAVE_OK_MSG : Code.SAVE_ERROR_MSG);
    }

    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestBody Map reqMap) {
        List<Long> ids = (List<Long>) reqMap.get("ids");
        boolean save = iSkillExcludeTeacherService.removeBatchByIds(ids);
        return new Result(save ? Code.DELETE_OK : Code.DELETE_ERROR, null, save ? Code.DELETE_OK_MSG : Code.DELETE_ERROR_MSG);
    }

    @PutMapping("/updateById")
    public Result updateById(@RequestBody SkillExcludeTeacher skillExcludeTeacher) {
        SkillExcludeTeacher byId = iSkillExcludeTeacherService.getById(skillExcludeTeacher.getId());
        if (byId == null) {
            return new Result(Code.UPDATE_ERROR, null, "不存在该教师");
        }
        if (!byId.getTeacherId().equals(skillExcludeTeacher.getTeacherId())) {
            if (checkRepeat(skillExcludeTeacher)) {
                return new Result(Code.UPDATE_ERROR, null, "已存在该教师，请勿重复添加");
            }
        }
        boolean update = iSkillExcludeTeacherService.updateById(skillExcludeTeacher);
        return new Result(update ? Code.UPDATE_OK : Code.UPDATE_ERROR, null, update ? Code.UPDATE_OK_MSG : Code.UPDATE_ERROR_MSG);
    }

    @Nullable
    private boolean checkRepeat(SkillExcludeTeacher skillExcludeTeacher) {
        QueryWrapper<SkillExcludeTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id", skillExcludeTeacher.getTeacherId());
        queryWrapper.eq("year_part", skillExcludeTeacher.getYearPart());
        queryWrapper.eq("quarter_num", skillExcludeTeacher.getQuarterNum());
        queryWrapper.eq("type", skillExcludeTeacher.getType());
        queryWrapper.eq("items_id", skillExcludeTeacher.getItemsId());
        long count = iSkillExcludeTeacherService.count(queryWrapper);
        return count > 0;
    }
}
